package mysql

import (
	"douyin/models"

	"go.uber.org/zap"
	"gorm.io/gorm"
)

// Favorite 点赞
func Favorite(videoId, userId int64) (err error) {
	//1. 用户是否点赞了视频
	isLike, err := WhetherUserLikeVideo(videoId, userId)
	//2.查不到数据，直接插入数据
	if err == ErrorQueryZeroRecord {
		err = nil
		favorite := &models.Favorite{
			UserId:  userId,
			VideoId: videoId,
			IsLike:  true,
		}
		tx := db.Debug().Model(&models.Favorite{}).Create(favorite)
		if tx.Error != nil {
			err = tx.Error
			return
		}
		return
	}
	if err != nil {
		zap.L().Error("mysql.WhetherUserLikeVideo failed", zap.Error(err))
		return
	}
	//3.查到数据isLike为true
	if isLike {
		return
	}
	//4.查到数据isLike为false
	if !isLike {
		//更新isLike
		tx := db.Debug().Model(&models.Favorite{}).Where("user_id = ? AND video_id = ?", userId, videoId).Update("is_like", true)
		if tx.Error != nil {
			err = tx.Error
			return
		}
		return
	}
	return
}

// UnFavorite 取消点赞
func UnFavorite(videoId, userId int64) (err error) {
	//1. 用户是否点赞了视频
	isLike, err := WhetherUserLikeVideo(videoId, userId)
	//2.查不到数据，直接返回，减少sql操作；也可以插入一条记录，其中记录isLike改为false
	if err == ErrorQueryZeroRecord {
		err = nil
		return
	}
	if err != nil {
		zap.L().Error("mysql.WhetherUserLikeVideo failed", zap.Error(err))
		return
	}
	//3.查到isLike为true
	if isLike {
		//更新isLike
		tx := db.Debug().Model(&models.Favorite{}).Where("user_id = ? AND video_id = ?", userId, videoId).Update("is_like", false)
		if tx.Error != nil {
			err = tx.Error
			return
		}
		return
	}
	//4.查到isLike为false
	if !isLike {
		return
	}
	return
}

// WhetherVideoLike 登录用户是否为视频点赞,为外部接口所调用
func WhetherVideoLike(videoId, myId int64) (like bool) {
	islike, err := WhetherUserLikeVideo(videoId, myId)
	if err == ErrorQueryZeroRecord {
		return false
	}
	if err != nil {
		return false
	}
	if !islike {
		return false
	}
	if islike {
		return true
	}
	return false
}

// WhetherUserLikeVideo 用户是否为视频点赞
func WhetherUserLikeVideo(videoId, userId int64) (isLike bool, err error) {
	tx := db.Debug().Model(&models.Favorite{}).Select("is_like").Where("user_id = ? AND video_id = ?", userId, videoId).First(&isLike)
	//1.查不到数据
	if tx.Error == gorm.ErrRecordNotFound {
		err = ErrorQueryZeroRecord
		return
	}
	//2.查询发生其他错误，默认未点赞
	if tx.Error != nil {
		isLike = false
		err = tx.Error
		return
	}
	//3.没进入上面的判断，说明查得到,isLike会被赋值为true或false
	return
}

// SelectFavorite 通过userId查询用户点赞视频的id列表
func SelectFavorite(userId int64) (vidIds []int64, err error) {
	//	select video_id from favorites where user_id =1 and is_like=true
	tx := db.Debug().Model(&models.Favorite{}).Select("video_id").Where("user_id=? AND is_like=?", userId, true).Find(&vidIds)
	//找不到记录，报错
	if len(vidIds) == 0 {
		return nil, ErrorQueryZeroRecord
	}
	//未知错误
	if tx.Error != nil {
		return nil, tx.Error
	}
	return
}

// VideoFavoriteCount 维护某一个视频的点赞总数
func VideoFavoriteCount(videoId int64, isAdd bool) (err error) {
	var str string
	var condition string
	if isAdd {
		str = "+"
		condition = ""
	} else {
		str = "-"
		condition = " AND favorite_count>0"
	}
	tx := db.Debug().Model(&models.Vid{}).Where("video_id = ?"+condition, videoId).Update("favorite_count", gorm.Expr("favorite_count"+str+" ?", 1))

	if tx.RowsAffected == 0 {
		err = ErrorUpdateFailed
		return
	}
	if tx.Error != nil {
		err = tx.Error
		return
	}
	return
}
